import config from '@/config'
import storage from '@/utils/storage'
import constant from '@/utils/constant'
import { login, logout, getInfo, wxLogin, sendCode, smsLogin } from '@/api/login'
import { getToken, setToken, removeToken, setStorage, removeStorage } from '@/utils/auth'
import { editUser, getUserInfoByUserId,editUserRealName,getSubordinateList } from '@/api/system/user'
import application from '../../static/const/application'

const baseUrl = config.baseUrl

const user = {
  state: {
    token: getToken(),
    name: storage.get(constant.name),
    avatar: storage.get(constant.avatar),
    roles: storage.get(constant.roles),
    permissions: storage.get(constant.permissions)
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, name) => {
      state.name = name
      storage.set(constant.name, name)
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
      storage.set(constant.avatar, avatar)
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
      storage.set(constant.roles, roles)
    },
    SET_PERMISSIONS: (state, permissions) => {
      state.permissions = permissions
      storage.set(constant.permissions, permissions)
    },
    SET_USER: (state, user) => {
      storage.set("profile", user)
    }
  },

  actions: {
    // 登录
    Login({ commit }, userInfo) {
      const username = userInfo.username.trim()
      const password = userInfo.password
      const code = userInfo.code
      const uuid = userInfo.uuid
      return new Promise((resolve, reject) => {
        login(username, password, code, uuid).then(res => {
          setToken(res.token)
          commit('SET_TOKEN', res.token)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 微信登录
    WxLogin({ commit }, userInfo) {
      const code = userInfo.code
      const headPortrait = userInfo.headPortrait
      const name = userInfo.name
      const phone = userInfo.phone
      const invitationId = userInfo.invitationId
      return new Promise((resolve, reject) => {
        wxLogin({ code, headPortrait, name, phone, invitationId }).then(res => {
          setToken(res.data)
          //将token存入本地中

          commit('SET_TOKEN', res.data)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取用户信息
    GetInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        getInfo().then(res => {
          const user = res.user
          const avatar = (user == null || user.avatar == "" || user.avatar == null) ? require("@/static/images/profile.jpg") : baseUrl + user.avatar
          const username = (user == null || user.userName == "" || user.userName == null) ? "" : user.userName
          setStorage(application.user, user)
          if (res.roles && res.roles.length > 0) {
            commit('SET_ROLES', res.roles)
            commit('SET_PERMISSIONS', res.permissions)
          } else {
            commit('SET_ROLES', ['ROLE_DEFAULT'])
          }
          commit('SET_NAME', username)
          commit('SET_AVATAR', avatar)
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 退出系统
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        logout(state.token).then(() => {
          commit('SET_TOKEN', '')
          commit('SET_ROLES', [])
          commit('SET_PERMISSIONS', [])
          removeToken()
          removeStorage(application.user)
          storage.clean()
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 发送验证码
    SendCode({ commit }, phone) {
      return new Promise((resolve, reject) => {
        sendCode(phone).then(res => {
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 短信登录
    SmsLogin({ commit }, loginForm) {
      const phone = loginForm.phoneNumber
      const code = loginForm.code
      console.log(phone, code)
      return new Promise((resolve, reject) => {
        smsLogin({ phone, code }).then(res => {
          setToken(res.data)
          commit('SET_TOKEN', res.data)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 修改用户信息
    EditUser({ commit }, user) {
      return new Promise((resolve, reject) => {
        editUser(user).then(res => {
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 根据用户id获取用户信息
    GetUserInfoByUserId({ commit }, userId) {
      return new Promise((resolve, reject) => {
        getUserInfoByUserId(userId).then(res => {
          commit('SET_USER', res.data)
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 修改用户真实姓名
    EditUserRealName({ commit }, data) {
        return new Promise((resolve, reject) => {
            editUserRealName(data).then(res => {
            resolve(res)
            }).catch(error => {
            reject(error)
            })
        })
    },
    //获取当前用户的下级
    GetSubordinate({ commit }) {
        return new Promise((resolve, reject) => {
          getSubordinateList().then(res => {
            resolve(res)
            }).catch(error => {
            reject(error)
            })
        })
    }
  }
}

export default user
